
import { Router, RouteLocationNormalized, NavigationGuardNext } from 'vue-router';

/**
 * router扩展，页面切换动画
 */
const SessionStorage_key_Router_Extend_History = 'SessionStorage_key_Router_Extend_History';

function transitionExtend(orgin: Router): Router {
  const router = Object.create(orgin);


  router.customRouterData = {
    transitionName: '',
    history: JSON.parse(sessionStorage.getItem(SessionStorage_key_Router_Extend_History) || '[]') as string[],
  };

  const updateSessionStorage = () => {
    sessionStorage.setItem(SessionStorage_key_Router_Extend_History, JSON.stringify(router.customRouterData.history));
  };

  router.addRouterPath = (path: string) => {
    router.customRouterData.history.push(path);
    updateSessionStorage();
  };

  router.removeLastRouterPath = (n = 1) => {
    router.customRouterData.history.splice(-n, n);
    updateSessionStorage();
  };

  router.push = function (...args: any[]) {
    document.documentElement.scrollTop = 0;
    router.customRouterData.transitionName = 'slide_right';
    const location = args[0];
    router.addRouterPath(typeof location === 'string' ? location : location.path);
    router.__proto__.push.call(this, ...args);
  };

  router.replace = function (...args: any[]) {
    router.removeLastRouterPath();
    const location = args[0];
    router.addRouterPath(typeof location === 'string' ? location : location.path);
    router.customRouterData.transitionName = 'slide_left';
    router.__proto__.replace.call(this, ...args);
  };

  router.go = function (n: number) {
    if (n > 0) {
      console.error('router.go 暂不支持 前进 ！');
      return;
    }
    document.documentElement.scrollTop = 0;
    router.removeLastRouterPath(-n);
    router.customRouterData.transitionName = 'slide_right';
    router.__proto__.go.call(this, n);
  };

  router.back = function () {
    router.removeLastRouterPath();
    document.documentElement.scrollTop = 0;
    router.customRouterData.transitionName = 'slide_right';
    router.__proto__.go.call(this, -1);
  };

  router.forward = function () {
    console.error('router.forward 暂不支持 ！');
  };

  router.otherEventTransitionName = function (toPath: string) {
    if (router.customRouterData.transitionName) return;
    const toIndex = router.customRouterData.history.lastIndexOf(toPath);
    if (toIndex === -1 || router.customRouterData.history.length - toIndex !== 2) {
      router.addRouterPath(toPath);
      router.customRouterData.transitionName = 'slide_left';
    } else {
      router.removeLastRouterPath();
      router.customRouterData.transitionName = 'slide_right';
    }
  };

  let isInit = false;

  router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
    // 在每次路由切换前移除进度条
    if (isInit) {
      router.otherEventTransitionName(to.path);
    } else {
      isInit = true;
      if (!router.customRouterData.history.length) {
        router.addRouterPath(to.path);
      }
    }
    next();
  });

  // router.afterEach(() => {

  // });

  return router;
}

export default transitionExtend;
